﻿using DBUtil;
using Function;
using Model.Permission;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace DAL.Permission.Store
{
    public class UserRoleAssignDAL
    {
        DbHelperSQLP DbHelperSQL = new DbHelperSQLP(PubConstant.JHStore);
        /// <summary>
        /// Judge if is admin
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public bool IsAdmin(int uid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Permission.UserRoleAssign");
            strSql.Append(" where [UserID]=@UR_UID and RoleID=1");
            SqlParameter[] parameters = {
					new SqlParameter("@UR_UID", SqlDbType.Int,4)
			};
            parameters[0].Value = uid;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 删除某用户已分配的所有角色
        /// </summary>
        public bool DeleteByUser(int U_Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Permission.UserRoleAssign ");
            strSql.Append(" where [UserID]=@UR_UID");
            SqlParameter[] parameters = {
					new SqlParameter("@UR_UID", SqlDbType.Int,4)
			};
            parameters[0].Value = U_Id;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int UserRoleAssignID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Permission.UserRoleAssign");
            strSql.Append(" where UserRoleAssignID=@UserRoleAssignID");
            SqlParameter[] parameters = {
					new SqlParameter("@UserRoleAssignID", SqlDbType.Int,4)
			};
            parameters[0].Value = UserRoleAssignID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool ExistsByRole(int RoleID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Permission.UserRoleAssign");
            strSql.Append(" where RoleID=@RoleID");
            SqlParameter[] parameters = {
					new SqlParameter("@RoleID", RoleID)
			};

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(UserRoleAssignEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Permission.UserRoleAssign(");
            strSql.Append("UserID,RoleID)");
            strSql.Append(" values (");
            strSql.Append("@UserID,@RoleID)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.Int,4),
					new SqlParameter("@RoleID", SqlDbType.Int,4)};
            parameters[0].Value = model.UserID;
            parameters[1].Value = model.RoleID;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(UserRoleAssignEntity model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Permission.UserRoleAssign set ");
            strSql.Append("UserID=@UserID,");
            strSql.Append("RoleID=@RoleID");
            strSql.Append(" where UserRoleAssignID=@UserRoleAssignID");
            SqlParameter[] parameters = {
					new SqlParameter("@UserID", SqlDbType.Int,4),
					new SqlParameter("@RoleID", SqlDbType.Int,4),
					new SqlParameter("@UserRoleAssignID", SqlDbType.Int,4)};
            parameters[0].Value = model.UserID;
            parameters[1].Value = model.RoleID;
            parameters[2].Value = model.UserRoleAssignID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int UserRoleAssignID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Permission.UserRoleAssign ");
            strSql.Append(" where UserRoleAssignID=@UserRoleAssignID");
            SqlParameter[] parameters = {
					new SqlParameter("@UserRoleAssignID", SqlDbType.Int,4)
			};
            parameters[0].Value = UserRoleAssignID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string UserRoleAssignIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Permission.UserRoleAssign ");
            strSql.Append(" where UserRoleAssignID in (" + UserRoleAssignIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public UserRoleAssignEntity GetModel(int UserRoleAssignID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 UserRoleAssignID,UserID,RoleID from Permission.UserRoleAssign ");
            strSql.Append(" where UserRoleAssignID=@UserRoleAssignID");
            SqlParameter[] parameters = {
					new SqlParameter("@UserRoleAssignID", SqlDbType.Int,4)
			};
            parameters[0].Value = UserRoleAssignID;

            UserRoleAssignEntity model = new UserRoleAssignEntity();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public UserRoleAssignEntity DataRowToModel(DataRow row)
        {
            UserRoleAssignEntity model = new UserRoleAssignEntity();
            if (row != null)
            {
                if (row["UserRoleAssignID"] != null && row["UserRoleAssignID"].ToString() != "")
                {
                    model.UserRoleAssignID = int.Parse(row["UserRoleAssignID"].ToString());
                }
                if (row["UserID"] != null && row["UserID"].ToString() != "")
                {
                    model.UserID = int.Parse(row["UserID"].ToString());
                }
                if (row["RoleID"] != null && row["RoleID"].ToString() != "")
                {
                    model.RoleID = int.Parse(row["RoleID"].ToString());
                }
            }
            return model;
        }

    }
}
